//
//  SYS_OrderDetailGoodsView.m
//  System_App
//
//  Created by 吴健 on 2017/11/30.
//  Copyright © 2017年 czy001. All rights reserved.
//

#import "SYS_OrderDetailGoodsView.h"
#import "SYS_OrderListCell.h"
#import "SYS_GoodsDetailVC.h"

@interface SYS_OrderDetailGoodsView()<UITableViewDelegate, UITableViewDataSource>
{
    UIImageView *imv_head;
    UILabel     *lb_name;
    UILabel     *lb_date;
    
    //运费标题
    UILabel     *lb_weightTitle;
    UILabel     *lb_weightValue;
    //安装费
    UILabel     *lb_installTitle;
    UILabel     *lb_installValue;
    //优惠金额
    UILabel     *lb_couponTitle;
    UILabel     *lb_couponValue;
    //实付款
    UILabel     *lb_actuallyTitle;
    UILabel     *lb_actuallyValue;
    UIView      *v_line;
    //订单编号
    UILabel     *lb_orderNumber;
}
@property (nonatomic, strong) SYS_TableView *mainTableView;
@end

@implementation SYS_OrderDetailGoodsView

- (id)initWithFrame:(CGRect)frame
{
    self=[super initWithFrame:frame];
    if (self) {
        self.backgroundColor=[UIColor whiteColor];
        
        imv_head = [[UIImageView alloc] init];
        imv_head.image = WJIMAGE(@"public_people");
        imv_head.layer.cornerRadius=15;
        imv_head.layer.masksToBounds=YES;
        
        lb_name = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 100, 20)];
        lb_name.textColor = SubTitleColor;
        lb_name.text = @"追骆驼的鱼";
        lb_name.minimumScaleFactor = 12;
        lb_name.font = DSFONT_MIN;
        
        lb_date = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 100, 20)];
        lb_date.textColor = SubTitleColor;
        lb_date.textAlignment = NSTextAlignmentRight;
        lb_date.text = @"2017-8-15 17:00:00";
        lb_date.font = DSFONT_MIN;
        
        _mainTableView=[[SYS_TableView alloc] initWithFrame:CGRectMake(0, 40, SCREENWIDTH, 200) style:UITableViewStylePlain];
        _mainTableView.delegate=self;
        _mainTableView.dataSource=self;
        _mainTableView.scrollEnabled=NO;
        _mainTableView.separatorStyle=UITableViewCellSeparatorStyleNone;
        
        lb_weightTitle = [[UILabel alloc] init];
        lb_weightTitle.textColor = DSCOLOR_GARY_TITLE;
        lb_weightTitle.text=@"运费(快递/物流)";
        lb_weightTitle.font = [UIFont systemFontOfSize:12];
        
        lb_weightValue = [[UILabel alloc] init];
        lb_weightValue.textColor = DSCOLOR_GARY_TITLE;
        lb_weightValue.text = @"￥10.00";
        lb_weightValue.textAlignment = NSTextAlignmentRight;
        lb_weightValue.font = [UIFont systemFontOfSize:13];
        
        lb_installTitle = [[UILabel alloc] init];
        lb_installTitle.textColor = DSCOLOR_GARY_TITLE;
        lb_installTitle.text = @"安装费";
        lb_installTitle.font = [UIFont systemFontOfSize:12];
        
        lb_installValue = [[UILabel alloc] init];
        lb_installValue.textColor = DSCOLOR_GARY_TITLE;
        lb_installValue.text = @"￥0.00";
        lb_installValue.textAlignment = NSTextAlignmentRight;
        lb_installValue.font = [UIFont systemFontOfSize:13];
        
        lb_couponTitle = [[UILabel alloc] init];
        lb_couponTitle.textColor = DSCOLOR_GARY_TITLE;
        lb_couponTitle.text = @"优惠金额";
        lb_couponTitle.font = [UIFont systemFontOfSize:12];
        
        lb_couponValue = [[UILabel alloc] init];
        lb_couponValue.textColor = DSCOLOR_GARY_TITLE;
        lb_couponValue.text = @"￥0.00";
        lb_couponValue.textAlignment = NSTextAlignmentRight;
        lb_couponValue.font = [UIFont systemFontOfSize:13];
        
        lb_actuallyTitle = [[UILabel alloc] init];
        lb_actuallyTitle.text = @"实付款";
        lb_actuallyTitle.font = DSFONT;
        
        lb_actuallyValue = [[UILabel alloc] init];
        lb_actuallyValue.textColor = DSCOLOR_RED;
        lb_actuallyValue.textAlignment = NSTextAlignmentRight;
        lb_actuallyValue.text = @"￥180.00";
        lb_actuallyValue.font = [UIFont systemFontOfSize:13];
        
        v_line = [UIView new];
        v_line.backgroundColor = DSCOLOR_GRAY_BACKGROUND;
        
        lb_orderNumber = [UILabel new];
        lb_orderNumber.text = @"订单编号：DD2017156832478234";
        lb_orderNumber.textColor = DSCOLOR_GARY_TITLE;
        lb_orderNumber.font = [UIFont systemFontOfSize:13];
        
        [self sd_addSubviews:@[imv_head,lb_name,lb_date,_mainTableView,lb_weightTitle,lb_weightValue,lb_installTitle,lb_installValue,lb_couponTitle,lb_couponValue,lb_actuallyTitle,lb_actuallyValue,v_line,lb_orderNumber]];
    }
    return self;
}

- (void)setDetailModel:(SYS_OrderDetailModel *)detailModel{
    _detailModel = detailModel;
    [_mainTableView reloadData];
    
    imv_head.sd_layout
    .leftSpaceToView(self, 10)
    .topSpaceToView(self, 7.5)
    .widthIs(25)
    .heightIs(25);
    
    lb_date.sd_layout
    .rightSpaceToView(self, 10)
    .topSpaceToView(self, 10)
    .widthIs(100)
    .heightIs(20);
    
    lb_name.sd_layout
    .leftSpaceToView(imv_head, 10)
    .topSpaceToView(self, 10)
    .rightSpaceToView(lb_date, 5)
    .heightIs(20);
    
    _mainTableView.sd_layout
    .leftSpaceToView(self, 0)
    .topSpaceToView(imv_head, 10)
    .rightSpaceToView(self, 0)
    .heightIs(_mainTableView.contentSize.height);
    
    lb_weightTitle.sd_layout
    .leftSpaceToView(self, 10)
    .topSpaceToView(_mainTableView, 5)
    .widthIs(100)
    .heightIs(20);
    
    lb_weightValue.sd_layout
    .rightSpaceToView(self, 10)
    .topEqualToView(lb_weightTitle)
    .widthIs(150)
    .heightIs(20);
    
    lb_installTitle.sd_layout
    .leftSpaceToView(self, 10)
    .topSpaceToView(lb_weightTitle, 5)
    .widthIs(100)
    .heightIs(20);
    
    lb_installValue.sd_layout
    .rightSpaceToView(self, 10)
    .topEqualToView(lb_installTitle)
    .widthIs(150)
    .heightIs(20);
    
    lb_couponTitle.sd_layout
    .leftSpaceToView(self, 10)
    .topSpaceToView(lb_installTitle, 5)
    .widthIs(100)
    .heightIs(20);
    
    lb_couponValue.sd_layout
    .rightSpaceToView(self, 10)
    .topEqualToView(lb_couponTitle)
    .widthIs(150)
    .heightIs(20);
    
    lb_actuallyTitle.sd_layout
    .leftSpaceToView(self, 10)
    .topSpaceToView(lb_couponTitle, 5)
    .widthIs(100)
    .heightIs(20);
    
    lb_actuallyValue.sd_layout
    .rightSpaceToView(self, 10)
    .topEqualToView(lb_actuallyTitle)
    .widthIs(150)
    .heightIs(20);
    
    v_line.sd_layout
    .leftSpaceToView(self, 0)
    .rightSpaceToView(self, 0)
    .topSpaceToView(lb_actuallyTitle, 5)
    .heightIs(1);
    
    lb_orderNumber.sd_layout
    .leftSpaceToView(self, 10)
    .rightSpaceToView(self, 10)
    .topSpaceToView(v_line, 5)
    .heightIs(20);
    
    [self setupAutoHeightWithBottomView:lb_orderNumber bottomMargin:5];
    
    [self refreshUI];
}

- (void)refreshUI{
    [imv_head sd_setImageWithURL:[NSURL URLWithString:_detailModel.userLogo] placeholderImage:WJIMAGE(@"public_people")];
    lb_name.text = _detailModel.nickName;
    NSString *sCreatTime = [DSUtility dateStrFromCstampTime:_detailModel.payTime.integerValue withDateFormat:@"yyyy.MM.dd HH:mm:ss"];
    lb_date.text = sCreatTime;
    lb_weightValue.text = FORMAT(@"￥%.2f", _detailModel.freight.floatValue);
    //安装费
    float installPrice = 0;
    //优惠金额
    float couponPrice = 0;
    //总结
    float totalPrice = 0;
    //实付款
    float realityPrice = 0;
    for (int i=0; i<_detailModel.orderItems.count;i++) {
        OrderItems *model = _detailModel.orderItems[i];
        installPrice+= [model.servicePrice floatValue];
        couponPrice+= [model.discountPrice floatValue];
        totalPrice+= model.price.floatValue * model.count.intValue;
    }
    realityPrice = totalPrice + installPrice + _detailModel.freight.floatValue - couponPrice;
    lb_installValue.text = FORMAT(@"￥%.2f", installPrice);
    lb_couponValue.text = FORMAT(@"￥%.2f", couponPrice);
    lb_actuallyValue.text = FORMAT(@"%.2f", realityPrice);
    lb_orderNumber.text = FORMAT(@"订单编号：%@", _detailModel.orderNumber);

}

#pragma mark -- UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    OrderItems *model = _detailModel.orderItems[indexPath.row];
    SYS_GoodsDetailVC *vc = [SYS_GoodsDetailVC new];
    vc.goodsId = model.itemTypeId;
    [[self viewController].navigationController pushViewController:vc animated:YES];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 100;
}

#pragma mark -- UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _detailModel.orderItems.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"SYS_OrderListCell";
    SYS_OrderListCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (!cell) {
        cell = [SYS_OrderListCell getOrderListCell];
    }
    OrderItems *model = _detailModel.orderItems[indexPath.row];
    cell.itemModel = model;
    return cell;
}

- (CGFloat)cellContentViewWith
{
    CGFloat width = [UIScreen mainScreen].bounds.size.width;
    
    // 适配ios7
    if ([UIApplication sharedApplication].statusBarOrientation != UIInterfaceOrientationPortrait && [[UIDevice currentDevice].systemVersion floatValue] < 8) {
        width = [UIScreen mainScreen].bounds.size.height;
    }
    return width;
}

#pragma mark -- Custom
//获取View所在的Viewcontroller方法
- (UIViewController *)viewController {
    for (UIView* next = [self superview]; next; next = next.superview) {
        UIResponder *nextResponder = [next nextResponder];
        if ([nextResponder isKindOfClass:[UIViewController class]]) {
            return (UIViewController *)nextResponder;
        }
    }
    return nil;
}

@end
